<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a href="javascript:;" :class="{active:sorter==='weight_desc'}" @click="onSorter('weight_desc')">推荐</a>
      <a href="javascript:;" :class="{active:sorter===''}" @click="onSorter('')">最新</a>
      <div class="logo"><img src="@/assets/logo.png" alt=""></div>
    </nav>

  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="getList"
  >
    <ArticleItem v-for="item in articles" :key="item.id" :item="item"></ArticleItem>
  </van-list>

  </div>
</template>

<script>

import { Article } from '@/api/article'
export default {
  name: 'ArticlePage',
  data () {
    return {
      articles: [],
      loading: false,
      finished: false,
      current: 0, // 页数
      pageTotal: 1, // 总页数
      sorter: 'weight_desc'
    }
  },
  // created () {
  //   this.getList()
  // },
  methods: {

    async getList () {
      // console.log('加载啦')
      if (this.current >= this.pageTotal) {
        this.$toast('没有更多啦')
        // 数据已全部加载完毕,List组件在底部显示没有更多了
        this.finished = true
        return
      }
      // 页面++
      this.current++
      // 发送请求获取数据
      const result = await Article({ current: this.current, sorter: this.sorter })
      // console.log(result)
      // 数组合并
      this.articles = [...this.articles, ...result.data.data.rows]
      // 设置总页数
      this.pageTotal = result.data.data.pageTotal
      // 设置 loading false-> List组件才会触发下一次 分页逻辑
      this.loading = false
    },
    onSorter (sorter) {
      this.articles = []
      this.current = 0
      this.sorter = sorter
      this.loading = true
      this.finished = false
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}

</style>
